package com.app.studentActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.app.R;
import com.app.bean.QingjiaBean;
import com.app.bean.ShiwuBean;
import com.app.bean.UserBean;
import com.app.util.DBHelper3;
import com.app.util.DBHelper4;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.bumptech.glide.Glide;

public class QingjiaDetail extends AppCompatActivity {

    private TextView name, content, time, username, date, status;
    private Button delete, edit;
    private ImageView imageView;
    private QingjiaBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qingjia_detail);
        initView();

//        bean = (QingjiaBean) getIntent().getSerializableExtra("bean");

        Integer id = (Integer) getIntent().getSerializableExtra("bean");
        if(ObjectUtils.isNotEmpty(id)){
            String string = SPStaticUtils.getString(id + "");
            bean=GsonUtils.fromJson(string, QingjiaBean.class);
            SPStaticUtils.remove(id + "");
        }
        name.setText(bean.getTitle());
        content.setText(bean.getContent());
        time.setText(bean.getTime());
        username.setText("请假人："+bean.getUsername());
        date.setText("请假时间："+bean.getTime());
        if(bean.getStatus().equals("0")){
            status.setText("状态：未批准");
        }else {
            status.setText("状态：已批准");
        }
//        imageView.setImageResource(bean.getImage());
        Glide.with(this).load(bean.getPic()).into(imageView);
        String user = SPStaticUtils.getString("user");
        UserBean userBean = GsonUtils.fromJson(user, UserBean.class);
        if (userBean.getUser_id().equals(bean.getUserid())) {
            delete.setVisibility(View.VISIBLE);
            edit.setVisibility(View.VISIBLE);
        } else if (userBean.getUser_id().substring(0, 1).equals("j") || userBean.getUser_id().equals("admin")) {
            delete.setVisibility(View.VISIBLE);
            edit.setVisibility(View.VISIBLE);
        } else {
            delete.setVisibility(View.GONE);
            edit.setVisibility(View.GONE);
        }

        delete.setOnClickListener(v -> {
            DBHelper4 dbHelper = new DBHelper4(getApplicationContext());
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            int idToDelete = bean.getId();
            String selection = "id=?";
            String[] selectionArgs = {String.valueOf(idToDelete)};
            int deletedRows = db.delete("qingjia", selection, selectionArgs);
            if (deletedRows > 0) {
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
            }
            db.close();
        });
        edit.setOnClickListener(v -> {
            //跳转界面，并把数据传递过去，传递数据的名字是bean

            SPStaticUtils.put( bean.getId()+"", GsonUtils.toJson(bean));
            Intent intent = new Intent(this, QingjiaAdd.class);
            intent.putExtra("bean", bean.getId());
            startActivityForResult(intent, 0);
        });
    }

    private void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.getChildAt(0).setOnClickListener(v -> finish());
        ((TextView) toolbar.getChildAt(1)).setText("请假详情");

        name = findViewById(R.id.name);
        content = findViewById(R.id.content);
        time = findViewById(R.id.time);
        imageView = findViewById(R.id.detail_image);

        delete = findViewById(R.id.delete);
        edit = findViewById(R.id.edit);

        username = findViewById(R.id.username);
        date = findViewById(R.id.date);
        status = findViewById(R.id.status);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Integer id = (Integer) data.getSerializableExtra("bean");
            if(ObjectUtils.isNotEmpty(id)){
                String string = SPStaticUtils.getString(id + "");
                bean=GsonUtils.fromJson(string,QingjiaBean.class);
                SPStaticUtils.remove(id + "");
            }
            name.setText(bean.getTitle());
            content.setText(bean.getContent());
            time.setText(bean.getTime());
            if(bean.getStatus().equals("0")){
                status.setText("状态：未批准");
            }else {
                status.setText("状态：已批准");
            }
            Glide.with(this).load(bean.getPic()).into(imageView);
        }
    }
}